﻿/*******************************************************************************
#                    AnswerBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-18 20:07:18
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class AnswerBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateAnswerEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateAnswerEntity", entity);
        }

        private object InsertAnswerEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertAnswerEntity", entity);
        }

        private object DeleteAnswerEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteAnswerEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectAnswerEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                SqlMap.BeginTransaction();
                if (entity.EntityState == EntityState.New)
                {
                    QuestionBroker questionBroker = new QuestionBroker();
                    InsertAnswerEntity(entity);
                    questionBroker.UpdateQuestionStatue((entity as AnswerEntity).QuestionOID, QuestionStatus.HasAnswer);
                    entity.EntityState = EntityState.Old;
                }
                else
                {
                    UpdateAnswerEntity(entity);
                }

                SqlMap.CommitTransaction();
            }
            catch (Exception ex)
            {
                SqlMap.RollBackTransaction();
                throw ex;
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            AnswerEntity entity = new AnswerEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteAnswerEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method
        public IList<AnswerEntity> GetAnswerByQuestionOID(string questionOID)
        {
            return SqlMap.QueryForList<AnswerEntity>("SelectAnswerEntitiesByQuestion", questionOID);
        }

        public IList<AnswerEntity> GetLastAnswersByQuestionOID(string questionOID, int Count)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["PREFIX"] = Count > 0 ? "TOP " + Count.ToString() + " * " : "*";
            htKeys["OID"] = questionOID;

            return SqlMap.QueryForList<AnswerEntity>("SelectLastAnswerEntitiesByQuestion", htKeys);
        }

        public int GetQuestionAnswerCount(string questionOID)
        {
            return SqlMap.QueryForObject<int>("GetQuestionAnswerCount", questionOID);
        }

        public int GetUserReplyCount(string UserOID)
        {
            return SqlMap.QueryForObject<int>("GetUserReplyCount", UserOID); ;
        }

        public int GetUserAcceptCount(string UserOID)
        {
            return SqlMap.QueryForObject<int>("GetUserAcceptCount", UserOID); ;
        }

        public AnswerEntity GetRecentReply(string questionOID)
        {
            return SqlMap.QueryForObject<AnswerEntity>("GetRecentReply", questionOID);
        }
        #endregion
    }
}